<?php

namespace Admin\Controller;

class AdminMenuController extends __Controller {

    protected $form = array(
        'title' => array(
            'title' => '菜单标题',
            'type' => 'text',
            'name' => 'title'
        ),
        'url' => array(
            'title' => 'URL',
            'remark' => '[控制器/方法]，不支持参数',
            'type' => 'text',
            'name' => 'url'
        ),
        'hide' => array(
            'title' => '是否隐藏',
            'remark' => '如果选择是，该菜单将不显示在导航中，但仍参与权限控制',
            'type' => 'select',
            'name' => 'hide',
            'options' => array(
                0 => '显示',
                1 => '隐藏'
            )
        ),
        'sort' => array(
            'title' => '排序',
            'remark' => '值越大排越前',
            'type' => 'number',
            'name' => 'sort'
        )
    );

    public function index() {
        $this->title = $this->meta_title = '后台菜单管理';
        $this->description = '菜单将做为后台导航系统的展示，但同时在后台权限配置中也发挥作用。<br>菜单最多显示三级：顶级菜单将显示在顶部导航，二级、三级菜单显示在左侧边栏导航';

        $menus = D('AdminMenu')->tree();
        $this->assign('_list_',$menus);
        $this->display();
    }

    public function tree($items,$depth) {
        $this->assign('_items_',$items);
        $this->assign('depth',$depth);
        $this->display('AdminMenu/tree');
    }

    public function add(){
        if(IS_POST) {
            if(!$this->model->create($_POST,1))
                $this->error($this->model->getError());

            $this->_set();

            if(!$this->model->add())
                $this->error($this->model->getError());
            $this->success('添加成功。');
            exit;
        }
        else {
            $pid = I('get.pid');
            if($pid) {
                $this->title = '添加子菜单';
                $this->form['pid'] = array(
                    'type' => 'hidden',
                    'name' => 'pid',
                    'value' => $pid
                );
            }
            parent::add();
        }
    }

    public function edit($id) {
        if(IS_POST){
            if(!$this->model->create($_POST,2))
                $this->error($this->model->getError());

            $this->_set();

            if($this->model->save() === false)
                $this->error($this->model->getError());
            $this->success('修改成功。');
            exit;
        }
        parent::edit($id);
    }

    /**
     * 用来计算url，并且将值付给模型
     */
    private function _set() {
        $url = I('post.url');
        if(strpos($url,'/') === false) $this->error('必须按“控制器/方法?参数”的格式写菜单地址。');

        $path = $url;
        $query = '';
        if(strpos($url,'?')) list($path,$query) = explode('?',$url);

        $path = explode('/',$path);
        $action = array_pop($path);
        $controller = array_pop($path);

        $path = U($controller.'/'.$action);
        $root = $_SERVER['DOCUMENT_ROOT'];
        $base = SITE_PATH;
        $subdir = str_replace($root,'',$base);
        if($subdir !== '' && strpos($url,$subdir) === 0) {
            $path = substr($path,strlen($subdir)); // 如果thinkphp安装在子目录中，可以识别子目录，并只取url中非子目录部分
        }

        $this->model->controller = $controller;
        $this->model->action = $action;
        $this->model->path = md5_16($path);
        $this->model->query = $query;
    }

    public function delete($id) {
        if($this->model->where(array('pid' => $id))->count('id'))
            $this->error('该菜单有子菜单，请先删除子菜单。');

        if($this->model->where(array('id' => $id))->delete() === false)
            $this->error($this->model->getError());
        $this->success('删除成功');
    }

    public function sort($id, $value)
    {
        parent::sort($id, $value); // TODO: Change the autogenerated stub
    }
}